package com.itheima.controller;

import com.itheima.pojo.KsClass;
import com.itheima.pojo.dto.LoginDTO;
import com.itheima.pojo.User;
import com.itheima.service.KsClassService;
import com.itheima.service.UserService;
import com.itheima.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/v1/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private KsClassService ksClassService;

    @GetMapping("/{mobile}")
    public ResponseResult findNameType(@PathVariable("mobile") String mobile) {
        User user = userService.findUserByMobile(mobile);
        if (user.getRoleId() == 2) {
            List<KsClass> classNames = ksClassService.findClassByMobile(mobile);
            return new ResponseResult(0, "操作成功", classNames);
        }
        if (user.getRoleId() == 1) {
            List<KsClass> ksClasses = ksClassService.findAll();
            return new ResponseResult(0, "操作成功", ksClasses);
        }
       return new ResponseResult(1, "操作失败", "");
    }

    @PostMapping("/in")
    public ResponseResult login(@RequestBody LoginDTO loginDTO, HttpSession session) {
        User user = userService.login(loginDTO);
        if (user != null) {
            session.setAttribute("user",user);
            return new ResponseResult(0,"操作成功","");
        }else {
            return new ResponseResult(1,"操作失败","");
        }
    }
}
